<?php

class Customers_model extends Model {
	
	
	var $customer_id;
	var $last_name;
	var $first_name;
	var $middle;
	var $dob;
	var $customer_gender_id;
	var $race_id;
	var $home_address;
	var $home_city_id;
	var $state_id;
	var $zip_code;
	var $home_phone;
	var $work_name;
	var $work_phone;
	var $photo_id_number;
	var $photo_id_type_id;
	var $photo_id_state;
	var $height;
	var $weight;
	var $eye_color_id;
	var $hair_color_id;
	var $customer_note;
	
	function Customers_model()
	{
		parent::Model();
	}
	
	// This function compares an array of data to the table fields, and deletes any items in the data
	// that are not a field in the table
	
	protected function filterInputArray($table, $data, $xss_clean = false, $caps = true){
        $fields = $this->db->list_fields($table);

        foreach ($data as $k => $v) {
                if(in_array($k, $fields) == false){
                        unset($data[$k]);
                } 
				else 
				{
                        if($xss_clean === true) $data[$k] = $this->input->xss_clean($v);
                        if($caps === true) $data[$k] = strtoupper($v); 	// convert to CAPS
                }
        }

        return $data;
	}

	function general()
	{
		$phoneJS = 'onkeydown="javascript:backspacerDOWN(this,event);" onkeyup="javascript:backspacerUP(this,event);"';
		$dateJS = 
		
		// Set up the parameters for the form_helper function that displays the fields 
		// in the customer add and customer update view
		$data['flastname'] 		= array('name'=>'last_name','size'=>18 ); 
		$data['ffirstname'] 	= array('name'=>'first_name','size'=>12 ); 
		$data['fmiddle'] 		= array('name'=>'middle', 'size'=>12 ); 
		$data['fmonth'] 		= array('name'=>'month','size'=>1); 	                                
		$data['fday'] 			= array('name'=>'day', 'size'=>1 );                             
		$data['fyear'] 			= array('name'=>'year','size'=>2 );                             
		$data['faddress'] 		= array('name'=>'home_address', 'size'=>30 ); 	                                
		$data['fcity'] 			= array('name'=>'home_city'); 
		$data['fzip'] 			= array('name'=>'zip_code', 'size'=>10 );    
		$data['fphone'] 		= array('name'=>'home_phone', 'size'=>11,'js'=>$phoneJS ); 
		$data['fworkname'] 		= array('name'=>'work_name', 'size'=>30); 
		$data['fworkphone'] 	= array('name'=>'work_phone','size'=>11,'js'=>$phoneJS );     
		$data['fphotoidnumber'] = array('name'=>'photo_id_number', 'size'=>10 ); 
		$data['fphotoidstate'] 	= array('name'=>'photo_id_state', 'size'=>6 ); 
		$data['fheight'] 		= array('name'=>'height', 'size'=>2);    
		$data['fweight'] 		= array('name'=>'weight', 'size'=>2 );                   
		$data['fnote']  		= array('name'=>'customer_note', 'rows'=>2,'cols'=>50); 

		// Get the options to poppulate the dropdown lookup fields
		$fGendersOptions = $this->get_Genders();
		$fRaceOptions = $this->get_Races();
		$fCityOptions = $this->get_Cities();
		$fStateOptions = $this->get_States();
		$fEyeColorOptions = $this->get_EyeColors();
		$fHairColorOptions = $this->get_HairColors();
		$fPhotoIdOptions = $this->get_photoIdTypes();
      
      // setup the lookup fields
      $data['fgender']	= array('name'=>'customer_gender_id', 'options' => $fGendersOptions, 'select' =>'');
      $data['frace']		= array('name'=>'race_id', 'options' => $fRaceOptions, 'select' =>'');
      $data['fcity']		= array('name'=>'home_city_id', 'options' => $fCityOptions, 'select' =>'');
      $data['fstate']		= array('name'=>'state_id', 'options' => $fStateOptions, 'select' => '');
      $data['feyecolor']	= array('name'=>'eye_color_id', 'options' => $fEyeColorOptions, 'select' => '');
      $data['fhaircolor']	= array('name'=>'hair_color_id', 'options' => $fHairColorOptions, 'select' => '');
      $data['fphotoidtype'] = array('name'=>'photo_id_type_id', 'options' => $fPhotoIdOptions, 'select' => '');
      
      // Retrieve the database fields
      $fields = $this->db->list_fields('customers');
      $fields[] = 'month';
      $fields[] = 'day';
      $fields[] = 'year';
      
      // Create an associative array of the table field names as key, and an empty value
      foreach ($fields as $k => $v)
         $empty_table[$v] = '';			
				
		// convert the array to objects and store it in $data['result']
		$data['result'] = parseArrayToObject($empty_table);	
		
		return $data;    	    
	}
	
	function get() {
		
		// If any of the following properties are set before customers_model->get() is called from the controller
		//  then we will include a where statement for each of the properties that have been set.
		if ($this->customer_id) {
            $this->db->where("customer_id", $this->customer_id);
        }
        if ($this->last_name) {
            $this->db->where("last_name", $this->last_name);
        }
        if ($this->first_name) {
            $this->db->where("first_name", $this->first_name);
        }
        if ($this->home_phone) {
            $this->db->where("home_phone", $this->home_phone);
        }
        
		$this->db->select('*');
		$this->db->select('customer_genders.customer_gender, races.race_description, home_cities.city');
		$this->db->select('states.state_abbreviation, eye_colors.eye_color, hair_colors.hair_color');

		$this->db->from('customers');
		$this->db->join('home_cities','customers.home_city_id = home_cities.home_city_id','left');
		$this->db->join('customer_genders','customers.customer_gender_id = customer_genders.customer_gender_id','left');
		$this->db->join('races','customers.race_id = races.race_id','left');
		$this->db->join('states','customers.state_id = states.state_id','left');
		$this->db->join('eye_colors','customers.eye_color_id = eye_colors.eye_color_id','left');
		$this->db->join('hair_colors','customers.hair_color_id = hair_colors.hair_color_id','left');
		
		
		// We will display our results in order by last name and then first name.
        $this->db->orderby("last_name, first_name");

		$query = $this->db->get();
		
		// If you set Customer_model->customer_id from your controller, then there will only be one row to return.
      if ($this->customer_id) {
         return ($query->row());		// returns an object
      }
      else
        	return $query->result();	// returns an array of objects
	}
	
	function save($data)
	{
		$update = FALSE;
		
		// Allows you to add a new customer as well as update an existing customer
		$db_array = $this->filterInputArray('customers', $data);
		
		if ($db_array['customer_id'] != '')		// If customer_id field is not blank, then it's an update
		{
			$update = TRUE;
			$customer_id = $db_array['customer_id'];
			
		}
		
		unset($db_array['customer_id']);
		
		// If Customers_model->customer_id was set in the controller, then we will update an existing record.
        
        
        if ($update) {
			
			$this->db->where("customer_id", $customer_id);
            $this->db->update("customers", $db_array);
        }

        else {
            $this->db->insert("customers", $db_array);
        }
        
    }
	
	function delete($id)
	{
		// function deletes a customer record based on the Id value
		// As long as Customers_model->customer_id was set in the controller, we will delete the record.
        
        $this->customer_id = $id;
        
		if ($this->customer_id) {
            $this->db->where("customer_id", $this->customer_id);
            $this->db->delete("customers");
        }
		
	}
	
	// Lookup Functions
	
	function get_Cities() {
			
		$query = $this->db->get('home_cities');
		if ($query->num_rows() > 0) {
						
			$query_result = $query->result_array();
			
			foreach ($query_result as $row)
			{
				$id = $row['home_city_id'];
				$value = $row['city'];
				$options_array[$id] = $value;
			}
			return $options_array;
		}
		else
			return FALSE;
	}
	function get_Genders() {
			
		$query = $this->db->get('customer_genders');
		if ($query->num_rows() > 0) {		
						
			$query_result = $query->result_array();
			
			foreach ($query_result as $row)
			{
				$id = $row['customer_gender_id'];
				$value = $row['customer_gender'];
				$options_array[$id] = $value;
			}
			return $options_array;
		}
		else
			return FALSE;
	}
	
	function get_Races() {
			
		$query = $this->db->get('races');
		if ($query->num_rows() > 0) {
						
			$query_result = $query->result_array();
			
			foreach ($query_result as $row)
			{
				$id = $row['race_id'];
				$value = $row['race_abbreviation'];
				$options_array[$id] = $value;
			}
			return $options_array;
		}
		else
			return FALSE;
	}
	
	function get_States() {
			
		$query = $this->db->get('states');
		if ($query->num_rows() > 0) {
						
			$query_result = $query->result_array();
			
			foreach ($query_result as $row)
			{
				$id = $row['state_id'];
				$value = $row['state_abbreviation'];
				$options_array[$id] = $value;
			}
			return $options_array;
		}
		else
			return FALSE;
	}
	
	function get_EyeColors() {
			
		$query = $this->db->get('eye_colors');
		if ($query->num_rows() > 0) {
						
			$query_result = $query->result_array();
			
			foreach ($query_result as $row)
			{
				$id = $row['eye_color_id'];
				$value = $row['eye_color'];
				$options_array[$id] = $value;
			}
			return $options_array;
		}
		else
			return FALSE;
	}
	
	function get_HairColors() {
			
		$query = $this->db->get('hair_colors');
		if ($query->num_rows() > 0) {
						
			$query_result = $query->result_array();
			
			foreach ($query_result as $row)
			{
				$id = $row['hair_color_id'];
				$value = $row['hair_color'];
				$options_array[$id] = $value;
			}
			return $options_array;
		}
		else
			return FALSE;
	}
	function get_photoIdTypes() {
			
		$query = $this->db->get('photo_id_types');
		if ($query->num_rows() > 0) {
						
			$query_result = $query->result_array();
			
			foreach ($query_result as $row)
			{
				$id = $row['photo_id_type_id'];
				$value = $row['photo_id_type'];
				$options_array[$id] = $value;
			}
			return $options_array;
		}
		else
			return FALSE;
	}	
}




/** lookup code from internet -> this goes on the view page

  echo '<select name="source" id="source">';
  $result = mysql_query("SELECT * FROM source");
  while($row = mysql_fetch_array($result)){
    echo '<option value="'.$row['source_id'].'"';
    if($client_info['source_id'] == $row['source_id']) echo ' selected';
    echo '>'.$row['source'].'</option>';
  }
  echo '</select>';
 */

?>